package com.tgky.ybzb.neo4j.mapper;

import com.tgky.ybzb.neo4j.entity.NodeEntityObj;
import com.tgky.ybzb.neo4j.entity.PersonWithRelationship;
import org.springframework.data.neo4j.repository.Neo4jRepository;

import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Set;

/**
 * @author Lixx
 * @date 2024/9/4 9:44
 */
public interface NodeObjMapper extends Neo4jRepository<NodeEntityObj, String> {
    /**
     * 根据id删除节点
     * @param id
     * @return
     */
    @Query("MATCH (n:NodeEntityObj {id: $id}) DETACH DELETE n return 1")
    int deleteNodeData(@Param("id") String id);

    /**
     * 报错节点之间的关系
     * @param nodeEntityObj
     * @return
     */
    @Query("MATCH (A:NodeEntityObj {id: $nodeEntityObj.__properties__.sourceId}), (B:NodeEntityObj {id: $nodeEntityObj.__properties__.targetId})\n" +
            "CREATE (A)-[r:RELATIONSHIP_TYPE {id: $nodeEntityObj.__id__, sourceId:$nodeEntityObj.__properties__.sourceId, targetId:$nodeEntityObj.__properties__.targetId}]->(B)")
    void createEdgeData(@Param("nodeEntityObj") NodeEntityObj nodeEntityObj);

    /**
     * 删除两个节点之间的关系
     * @param id
     */
    @Query("MATCH ()-[r]->()  WHERE r.id=$id  DELETE r")
    void deleteEdgeData(@Param("id") String id);

    /**
     * 获取所有节点信息
     * @return
     */
    @Query("match (n) return n")
    List<NodeEntityObj> getNodeList();

    /**
     * 获取所有匹配的节点
     * @return
     * @param condition
     * @param chartType
     * @param skip
     */
    @Query("match (n:NodeEntityObj)-[r:RELATIONSHIP_TYPE *$skip]-(m:NodeEntityObj) where n.chartType=$chartType And n.nodeContent =~ '.*$condition.*' return n")
    Set<NodeEntityObj> getAllNodeData(@Param("condition") String condition, @Param("chartType") String chartType, @Param("skip") String skip);

    /**
     * 获取所有匹配的节点关系
     * @param condition
     * @param chartType
     * @param skip
     * @return
     */
    @Query("match (n:NodeEntityObj)-[r:RELATIONSHIP_TYPE *$skip]-(m:NodeEntityObj) where n.chartType=$chartType And n.nodeContent =~ $condition return r")
    Set<NodeEntityObj> getAllEdgeData(@Param("condition") String condition, @Param("chartType") String chartType, @Param("skip") String skip);


    /**
     * 根据名称获取节点
     * @param nodeEntityObj
     * @return
     */
    @Query("match (n) where n.name= $nodeEntityObj.__properties__.name return n limit 1")
    NodeEntityObj getNodeByName(@Param("nodeEntityObj") NodeEntityObj nodeEntityObj);

    /**
     * 导入保存两个节点之间的关系
     * @param startNode
     * @param endNode
     * @param relationship
     */
    @Query("MATCH (A:NodeEntityObj {id: $startNode.__id__}), (B:NodeEntityObj {id: $endNode.__id__})\n" +
            "CREATE (A)-[r:RELATIONSHIP_TYPE {id: $relationship.__id__, start: $relationship.__properties__.start, end: $relationship.__properties__.end,shape:$relationship.__properties__.shape,type:$relationship.__properties__.type}]->(B)")
    void saveEdgeData(@Param("startNode") NodeEntityObj startNode,@Param("endNode") NodeEntityObj endNode,@Param("relationship") NodeEntityObj relationship);



//@Query("CREATE (n: $nodeObj.__id__ {id: $nodeObj.__id__, name:$nodeObj.__properties__.name, label:$nodeObj.__properties__.label, size:$nodeObj.__properties__.size, " +
//        "type:$nodeObj.__properties__.type, shape:$nodeObj.__properties__.shape, color:$nodeObj.__properties__.color, backgroundColor:$nodeObj.__properties__.backgroundColor, x:$nodeObj.__properties__.x, y:$nodeObj.__properties__.y," +
//        "nodeContent:$nodeObj.__properties__.nodeContent}) return n")
//    void saveNodeObjData(@Param("nodeObj") NodeEntityObj nodeObj);
}
